package com.bessystem.demo.web;

import com.bessystem.demo.domain.User;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class FileUploadController {
    @RequestMapping("/")
    public void main(HttpServletResponse res) throws IOException {
        res.sendRedirect("/index.html");
    }

    @RequestMapping("/hello")
    @ResponseBody
    public String hello(@RequestParam(name = "name", defaultValue = "unknown user") String name) {
        return "Hello " + name;
    }

    @RequestMapping("/user")
    @ResponseBody
    public User user() {
        User user = new User();
        user.setName("theonefx");
        user.setAge(666);
        return user;
    }

    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public void upload(@RequestParam("file") MultipartFile file,
                         @RequestParam(value = "description") String description,
                         @RequestParam(value = "depts") String depts,
                         @RequestParam(value = "roles") String roles,
                         HttpServletResponse res) {

        System.out.println("desc:" + description);
        System.out.println("depts:" + depts);
        System.out.println("roles:" + roles);
        System.out.println("file:" + file.getOriginalFilename());

        res.setStatus(200);
    }
}
